package com.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.annotation.OperationLog;
import com.mall.db.domain.Comment;
import com.mall.db.domain.Member;
import com.mall.db.service.CommentService;
import com.mall.db.service.MemberService;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wxd
 * @since 2021-09-22
 */
@Controller
@RequestMapping("/comment/ct")
public class CommentController extends BaseController<Comment>{

    @Resource(name = "commentService")
    private CommentService cs;

    @Resource(name = "memberService")
    private MemberService ms;

    @Override
    public Map<String, Object> find(Comment comment) {
        PageHelper.startPage(comment.getPage(),comment.getLimit());
        QueryWrapper<Comment> qw= new QueryWrapper<>();

        if (!StringUtils.isEmpty(comment.getGid())){
            qw.eq("gid",comment.getGid());
        }

        List<Comment> list = cs.list(qw);

        int i = 0;
        for (Comment c:list) {
            Member m = ms.getById(c.getMid());
            c.setMname(m.getUserName());
            list.set(i,c);
            i++;
        }

        PageInfo<Comment> page = new PageInfo<>(list,comment.getLimit());
        return this.findMap(page.getList(),page.getTotal());
    }

    @Override
    public Map<String, Object> add(Comment comment) {
        comment.setCreateTime((new Date()));
        boolean b = cs.save(comment);
        return this.iud("新增",b,comment);
    }

    @Override
    @OperationLog(value = "删除客户评论",type = 0)

    public Map<String,Object> delete(Comment comment){
        Map<String,Object> map = new HashMap<>();
        if (!StringUtils.isEmpty(comment.getId())){
            cs.removeById(comment.getId());
            map.put("is",true);
            map.put("msg","删除成功!");
        }else {
            map.put("is",false);
            map.put("msg","删除失败");

        }
        return map;
    }

}

